package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/grpc/gen/proxy2/bilinadmin"
	"bgs/util"
	"context"
)

// IdentifiyBrandAdminEmployee 识别品牌管理员员工
func (s *BrandServiceImp) IdentifiyBrandAdminEmployee(ctx context.Context, q *sqlc.Queries, brandAdminPtr *sqlc.BrandAdmin) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	cBrand := s.cacher.FetchBrand(brandAdminPtr.BrandID)
	sgID := cBrand.SgID
	userExt, err := q.GetUserExtByUserID(ctx, brandAdminPtr.UserID)
	if err != nil {
		return err
	}
	slog.Info("3 获取user ext ok")
	token := userExt.AccountToken

	req := &bilinadmin.EmployeeLoginReq{
		BrandId: sgID,
		Token:   token.String,
	}

	slog.Infof("LoginAdmin req:%v", req)

	ctxWithTimout, cancel := s.proxy.NewContextWith(ctx)
	defer cancel()

	reply, err := s.proxy.EmployeeCl.LoginAdmin(ctxWithTimout, req)
	slog.Infof("err=>%+v", err)
	if err != nil {
		return
	}
	brandAdminPtr.RemotePk = reply.EmpId
	slog.Infof("1 识别品牌管理员员工 ok")
	return
}
